package zero.tech.core.util;

import java.util.ArrayList;
import java.util.Collections;
import java.util.Comparator;
import java.util.List;
import java.util.Map;

public class SignUtil {

	public static String signMd5(Map<String, String> M,String secret) {
		String sign="";
		List<String> keys=new ArrayList<>();
		keys.addAll(M.keySet());
		Collections.sort(keys,new Comparator<String>() {

			@Override
			public int compare(String o1, String o2) {
				return o1.compareTo(o2);
			}
		});
		String stringA="";
		for(String key:keys) {
			String tmp=M.get(key);
			if(StringHelper.isNotEmpty(tmp)) {
				stringA+=key+"="+M.get(key)+"&";
			}
		}
		if(StringHelper.isNotEmpty(stringA)) {
			stringA+="key="+secret;
			System.out.println(stringA);
			sign=md5(stringA).toUpperCase();
		}
		return sign;
	}

	private static String md5(String stringA) {
		return MD5Util.md5(stringA);
	}

}
